import 'package:flutter/material.dart';
import '../helper/cacheHelper.dart';
import '../model/user.dart';

typedef UserSelectedCallback = void Function(User u1);

class UserSelectWidget extends StatefulWidget {
  @override
  UserSelectWidget(this.userSelectedCallback, this.authority);
  final UserSelectedCallback userSelectedCallback;
  final String authority;
  @override
  State<StatefulWidget> createState() => UserSelectState();
}

class UserSelectState extends State<UserSelectWidget> {
  //下拉菜单item点击之后获取到的值
  late User selectItemValue;
  /*DropDownState(){
    selectItemValue=getDropdownMenuItem()[0].value;
  }*/

  List<DropdownMenuItem> generateItemList() {
    List<DropdownMenuItem> items = [];
    for (var a in CacheHelper.userList) {
      if (a.Authority.indexOf(widget.authority) != -1) {
        var item1 = new DropdownMenuItem(
            key: Key(a.Guid), value: a, child: new Text(a.Name));
        items.add(item1);
      }
    }
    return items;
  }

  @override
  Widget build(BuildContext context) {
    //DropdownButtonHideUnderline：下拉菜单展示的内容处没有下划线
    return new DropdownButtonHideUnderline(
      child: new DropdownButton(
        hint: new Text('               '),
        //设置这个value之后,选中对应位置的item，
        //再次呼出下拉菜单，会自动定位item位置在当前按钮显示的位置处
        value: selectItemValue,
        items: generateItemList(),
        onChanged: (u1) {
          setState(() {
            widget.userSelectedCallback(u1);
            selectItemValue = u1;
          });
        },
      ),
    );
  }
}
